package com.poisonh.unifiedauth.dao;

import android.content.Context;

import com.poisonh.unifiedauth.model.UserInfoModel;
import com.poisonh.unifiedauth.utils.RealmUtils;

import java.util.List;

import io.realm.Realm;

/**
 * 数据库事务操作
 * Created by PoisonH on 2016/7/15.
 */
public class UserinfoDaoImpl implements UserinfoDao
{
    private Context mContext;
    private Realm mRealm;

    public UserinfoDaoImpl(Context context)
    {
        this.mContext = context;
        mRealm = RealmUtils.getInstance(mContext).getRealm();
    }

    /**
     * 添加一条数据集
     *
     * @param userInfoModel
     * @throws Exception
     */
    @Override
    public void addone(UserInfoModel userInfoModel) throws Exception
    {
        mRealm.beginTransaction();
        mRealm.copyToRealmOrUpdate(userInfoModel);
        mRealm.commitTransaction();
    }

    /**
     * 添加一个list集合
     *
     * @param list
     * @throws Exception
     */
    @Override
    public void addList(List<UserInfoModel> list) throws Exception
    {
        mRealm.beginTransaction();
        mRealm.copyToRealmOrUpdate(list);
        mRealm.commitTransaction();
    }

    /**
     * 根据id删除数据
     *
     * @param id
     * @throws Exception
     */
    @Override
    public void delete(int id) throws Exception
    {
        mRealm.beginTransaction();
        UserInfoModel mUserInfoModel = mRealm.where(UserInfoModel.class).equalTo("id", id).findFirst();
        mUserInfoModel.deleteFromRealm();
        mRealm.commitTransaction();
    }

    /**
     * 根据id查询一个集合
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public List<UserInfoModel> queryReturnList(String id) throws Exception
    {
        mRealm.beginTransaction();
        List<UserInfoModel> mUserInfoModel = mRealm.where(UserInfoModel.class).equalTo("id", id).findAll();
        mRealm.commitTransaction();
        return mUserInfoModel;
    }

    /**
     * 根据id查询一条数据
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public UserInfoModel queryReturnOne(String id) throws Exception
    {
        mRealm.beginTransaction();
        UserInfoModel mUserInfoModel = mRealm.where(UserInfoModel.class).equalTo("id", id).findFirst();
        return mUserInfoModel;
    }

    /**
     * 查询所有数据
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<UserInfoModel> findAll() throws Exception
    {
        mRealm.beginTransaction();
        List<UserInfoModel> mList = mRealm.where(UserInfoModel.class).findAll();
        mRealm.commitTransaction();
        return mList;
    }

    /**
     * 更新数据
     *
     * @param userInfoModel
     * @throws Exception
     */
    @Override
    public void updata(UserInfoModel userInfoModel) throws Exception
    {
        mRealm.beginTransaction();
        mRealm.copyToRealmOrUpdate(userInfoModel);
        mRealm.commitTransaction();
    }

    /**
     * 关闭数据库
     *
     * @throws Exception
     */
    @Override
    public void close() throws Exception
    {
        mRealm.close();
    }

    /**
     * 提交事务操作
     *
     * @throws Exception
     */
    @Override
    public void commit() throws Exception
    {
        mRealm.commitTransaction();
    }
}
